
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>


size_t
strlcpy(char *dst, const char *src, size_t siz)
{
	char *d = dst;
	const char *s = src;
	size_t n = siz;

	/* Copy as many bytes as will fit */
	if (n != 0) {
		while (--n != 0) {
			if ((*d++ = *s++) == '\0')
				break;
		}
	}

	/* Not enough room in dst, add NUL and traverse rest of src */
	if (n == 0) {
		if (siz != 0)
			*d = '\0';		/* NUL-terminate dst */
		while (*s++)
			;
	}

	return(s - src - 1);	/* count does not include NUL */
}

long int get_file_size(char *name)
{
	FILE *f=fopen(name,"r");
	
	if(!f)return -1;
	
	long int s;
	fseek(f,0,SEEK_END);
	s = ftell(f);
	fclose(f);
	
	return s;
}



char *target_list[500];
int num_targets=0;


#define DEFAULT_LIST	"list.txt"
#define DEFAULT_PATH		"./"

int load_target_list(char* filename)
{
	char buf[1024];
	char target[1024];
	char *ps,*ps2;
	FILE *f=fopen(filename,"r");
	if(!f)
		{
		printf("Error: cannot open file %s\n",filename);
		return 0;
		}

	while(!feof(f))
		{
		memset(buf,0,1024);
		memset(target,0,1024);
	
		fgets(buf,1023,f);
	
		if(strlen(buf)<=1)continue;
		if(buf[strlen(buf)-1]=='\n')
		buf[strlen(buf)-1]='\0';


		ps=ps2=NULL;


		ps=strstr(buf,"http://");
		if(!ps)continue;

		ps2=strchr(ps,' ');
		if(ps2)
			strlcpy(target,ps,ps2-ps+1);
		else
			strlcpy(target,ps,strlen(ps)+1);
	
		
		target_list[num_targets]=(char*)malloc(strlen(target));
		strcpy(target_list[num_targets],target);
		num_targets++;	
		}
		
	fclose(f);
	return 1;
	
}


int get_url(int index,char *name,char *url)
{
	char *s1,*s2;
	char wget[1024];
	char buf[1024];
	FILE *tmp;
	int j;

	memset(wget,0,1024);
	strcpy(wget,"wget ");
	strcat(wget,target_list[index]);
	strcat(wget," -q -O /tmp/mdcd_tmp.txt");
	system(wget);	
	
	
	
	tmp = fopen("/tmp/mdcd_tmp.txt","r");
	if(!tmp)
	{
		printf("Error: cannot open temporary file /tmp/mcdc_tmp.txt while getting webpage with target %s\n",target_list[index]);
		return;
	}
	
	while(!feof(tmp))
	{
		s1=s2=NULL;
		memset(buf,0,1024);
		fgets(buf,1023,tmp);
		s1=strstr(buf,"<b title=\"");
		if(s1)
		{
			memset(name,0,256);
			s1+=10;
			s2=strstr(s1,"\"");
			if(s2)
			{
				strncpy(name,s1,s2-s1);
				for(j=0;j<strlen(name);j++)
				{
					if((name[j]=='(') | (name[j]==')') | (name[j]==' ') | (name[j]=='"'))
					name[j]='_';
				}
			}
			continue;
		}

		s1=strstr(buf,"http://files.");
		if(s1)
		{
			s2=strstr(s1,"\"><");
			if(s2)
			{
			strncpy(url,s1,s2-s1);	
			break;	
			}
		}

	}
	
	fclose(tmp);
	
	if(!strlen(name) && !strlen(url))
	{
		printf("Cannot get name and file URL. Maybe, file doesn't exist.\n");
		return 0;
	}
	return 1;
}



void download_url(int index,char *path)
{
	char wget[1024];
	char name[256];
	char url[1024];
	
	memset(name,0,256);
	memset(url,0,1024);
	
	char rm[200];
	
	if(!get_url(index,name,url)) return;
	
	memset(wget,0,1024);

	strcpy(wget,"wget -O ");
	strcat(wget,path);
	strcat(wget,name);
	strcat(wget," -c ");
	strcat(wget,url);

	system(wget);
	
	sleep(2);
		

}

int download(char *path)
{
	int i;
	
	for(i=0;i<num_targets;i++)
	{
	
		download_url(i,path);
		sleep(1);

		system("rm /tmp/mdcd_tmp.txt");
		
	}
}




int main(int argc,char *argv[])
{
	char list_filename[200];
	char path[300];

	memset(list_filename,0,200);
	memset(path,0,300);

	int i;
	if(argc==2)
	{
		if((strstr(argv[1],"--help")) || (strstr(argv[1],"-h")))
		{
			printf("mdcd — is a console data.cod.ru download manager\n");
			printf("Parameters:\n");
			printf("-l <list_name> — use <list_name> as list of links. If not set, mdcd uses default \"list.txt\"\n");
			printf("-p <path> — save downloaded files to <path>. If not set, files will be saved into current directory.\n");
			printf("Example:\n");
			printf("mdcd -l mylist.txt -p ~/Downloads\n");
			return 0;
		}
	}
	for(i=1;i<argc;i++)
	{
		if(strstr(argv[i],"-l"))
		{
			strcpy(list_filename,argv[i+1]);
			continue;	
		}
		if(strstr(argv[i],"-p"))
		{
			strcpy(path,argv[i+1]);
			if(path[strlen(path)-1] != '/')
			{
				strcat(path,"/");
			}
			continue;
		}
	}
	
	if(!strlen(list_filename))
	{
		strcpy(list_filename,DEFAULT_LIST);
		printf("Using default list name - list.txt\n");	
	}
	if(!strlen(path))
	{
		strcpy(path,DEFAULT_PATH);
		printf("Saving files in current directory.\n");	
	}
	
	
	if(load_target_list(list_filename))
	{
		download(path);
	}
	
	
	return 0;

}

//
// by cryke 
//